package com.javacv.plus.extensions.image.steps;

import com.javacv.plus.core.processor.ProcessorContext;
import com.javacv.plus.core.processor.ProcessorStep;
import com.javacv.plus.extensions.image.ImageProcessor;
import com.javacv.plus.extensions.image.ProcessorResult;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.global.opencv_imgcodecs;

/**
 * 加载图像步骤
 */
@Slf4j
public class LoadImageStep implements ProcessorStep<ImageProcessor, ProcessorResult> {
    static {
        // 预加载必要的本地库
        Loader.load(opencv_imgcodecs.class);
    }
    @Override
    public ProcessorResult execute(ImageProcessor processor, ProcessorContext context) throws Exception {
        String inputPath = processor.getConfig().getInputPath();
        log.debug("加载图像: {}", inputPath);

        // 使用 OpenCV 加载图像
        Mat image = opencv_imgcodecs.imread(inputPath);
        
        if (image == null || image.empty()) {
            throw new RuntimeException("无法加载图像: " + inputPath);
        }

        // 设置到处理器和上下文
        processor.setSourceImage(image);
        context.setAttribute("currentImage", image);
        context.setAttribute("processedImage", image);

        log.debug("图像加载成功，尺寸: {}x{}", image.cols(), image.rows());
        return null;
    }

    @Override
    public String getStepName() {
        return "LoadImage";
    }

    @Override
    public int getPriority() {
        return 1; // 最高优先级，确保首先执行
    }
} 